#include "imtPublic.h"
#include "imtActionManager.h"
#include "imtActionTypes.h"

namespace iMate {	

	void ActionManager::destroyAction(IAction* action)
	{
		if (action)
		{
			action->terminate();
			action->clearDecoratee();
			action->clearData();

			int type = action->getActionType();
			if (iMateIsMemAlwaysActionType(type))
			{
				mActionPool[type].push_back(action);
			}
			else
			{
				delete action;
			}
		}
	}
	void ActionManager::destroyAllActions()
	{
		for (ActionPool::iterator it = mActionPool.begin();
			it != mActionPool.end(); ++it)
		{
			for (size_t i=0; i<it->second.size(); ++i)
			{
				delete it->second[i];
			}
		}
		mActionPool.clear();
	}
	IAction* ActionManager::getActionByType(int type)
	{
		IAction* ret = 0;
		ActionPool::iterator it = mActionPool.find(type);
		if (it != mActionPool.end() && !it->second.empty())
		{
			ret = it->second.back();
			it->second.pop_back();
		}
		return ret;
	}

	ActionManager& ActionManager::getSingleton(void)
	{
		assert( ms_Singleton );  return ( *ms_Singleton );
	}
	ActionManager* ActionManager::getSingletonPtr(void)
	{
		return ms_Singleton;
	}

}//
namespace Ogre {

	template<> iMate::ActionManager* Singleton<iMate::ActionManager>::ms_Singleton = NULL;

}